package top.oauth2.common.response;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/8/17 10:00
 */
public class ResponseMessage {

    private static final String CODE_KEY = "code";
    private static final String DATA_KEY = "data";
    private static final String MSG_KEY = "message";
    private static final String TIME_KEY = "timestamp";

    private final Message message;
    private HttpServletResponse response;


    public ResponseMessage() {
        this.message = new Message();
    }
    public ResponseMessage(Message message) {
        this.message = message;
    }
    public ResponseMessage(HttpServletResponse response) {
        this.message = new Message();
        this.response = response;
    }

    public static ResponseMessage builder(){
        return new ResponseMessage();
    }

    public static ResponseMessage builder(Message message){
        return new ResponseMessage(message);
    }

    public static ResponseMessage builder(HttpServletResponse response){
        return new ResponseMessage(response);
    }

    public ResponseMessage setHttpServletResponse(HttpServletResponse response){
        this.response = response;
        return this;
    }

    public ResponseMessage success(String msg){
        message.put(CODE_KEY, MessageCode.SUCCESS.code);
        message.put(MSG_KEY, msg);
        message.put(TIME_KEY, System.currentTimeMillis());
        return this;
    }

    public ResponseMessage success(){
        message.put(CODE_KEY, MessageCode.SUCCESS.code);
        message.put(MSG_KEY, MessageCode.SUCCESS.message);
        message.put(TIME_KEY, System.currentTimeMillis());
        return this;
    }

    public ResponseMessage error(String msg){
        message.put(CODE_KEY, MessageCode.FAIL.code);
        message.put(MSG_KEY, msg);
        message.put(TIME_KEY, System.currentTimeMillis());
        return this;
    }

    public ResponseMessage error(){
        message.put(CODE_KEY, MessageCode.FAIL.code);
        message.put(MSG_KEY, MessageCode.FAIL.message);
        message.put(TIME_KEY, System.currentTimeMillis());
        return this;
    }

    public ResponseMessage style(int code, String msg){
        message.put(CODE_KEY, code);
        message.put(MSG_KEY, msg);
        return this;
    }

    public ResponseMessage put(String key, Object val){
        message.put(key, val);
        return this;
    }

    public Message message(){
        return message;
    }

    public void returnMessage(){
        if (response == null){
            throw new NullPointerException("HttpServletResponse 不能为空");
        }
        try {
            responseObject(message, response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 返回消息
     * @param msg 返回消息
     * @param response 返回体
     */
    public static void responseObject(Object msg, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(),msg);
    }
}
